﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Text;

namespace Jy123.Web.Vote
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class VoteComment : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            int userID;//暂时写死
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                context.Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            string msg = string.Empty;

            string strHtml = context.Cache["user_votecomment_row"].ToString();
            string strDel = @"<a href=""javascript:myAjax('/Vote/VoteCommentDel.ashx','CommentID='+$CommentID$);"" class=""imgdel"" ><img src=""/images/del_blog.gif"" alt=""删除"" /></a>";
            
            string strComment = HttpUtility.UrlDecode(context.Request["comment"]);
            string strVoteID = context.Request["voteID"];
            if (!string.IsNullOrEmpty(strComment))
            {
                int newCommentID = 0;
                Jy123.Model.User_VoteComm mVoteComm = new Jy123.Model.User_VoteComm();
                Jy123.BLL.User_VoteComm bllVoteComm = new Jy123.BLL.User_VoteComm();
                mVoteComm.FUser = userID;
                mVoteComm.FVote = Convert.ToInt32(strVoteID);
                mVoteComm.CommentContent = strComment;
                mVoteComm.PostType = 1;
                mVoteComm.AddTime = DateTime.Now;
                newCommentID = bllVoteComm.Add(mVoteComm);
                //msg = @"评论成功!<script>AppendAjax('voteCommentUl','')</script>,ok";
                //msg = @"评论成功!,ok";
                if (newCommentID > 0)
                {
                    //加入动态
                    Jy123.Model.User_Main mUserForState = new Jy123.Model.User_Main();
                    Jy123.Model.User_Main mUserFrd = new Jy123.Model.User_Main();
                    Jy123.BLL.User_Main bllUserForState = new Jy123.BLL.User_Main();
                    Jy123.Model.User_Vote mVote = new Jy123.BLL.User_Vote().GetModel(Convert.ToInt32(strVoteID));
                    mUserFrd = bllUserForState.GetModel(Convert.ToInt32(mVote.FUser));
                    Jy123.Model.User_VoteComm mVoteCommState = new Jy123.BLL.User_VoteComm().GetModel(newCommentID);
                    new Jy123.BLL.Public.State().CreateCommentVoteState(mUserForState, mUserFrd, mVote, mVoteCommState);
                    //加入动态
                    strHtml = strHtml.Replace("$imgdel$", strDel);
                    strHtml = strHtml.Replace("$CommentID$", newCommentID.ToString());
                    strHtml = strHtml.Replace("$AddTime$",DateTime.Now.ToString());
                    strHtml = strHtml.Replace("$UserName$",new Jy123.BLL.User_Main().GetModel(userID).UserName);
                    strHtml = strHtml.Replace("$UserID$",userID.ToString());
                    //根据用户ID判断用户是否有头像,如果没有头像就返回默认头像地址                
                    string userPortrait = new Jy123.BLL.User.SPace().DefaultPortrait(userID);

                    strHtml = strHtml.Replace("$Portrait$",userPortrait);
                    strHtml = strHtml.Replace("$CommentContent$", strComment);
                    context.Response.ContentType = "text/plain";
                    context.Response.Write(strHtml);
                }
            }
            
        }
        /// <summary>
        /// 投票评论列表
        /// </summary>
        /// <param name="voteID">投票ID</param>
        /// <returns>字符串</returns>
        //private string VoteCommentList(Jy123.Model.User_VoteComm mVoteComm, HttpContext context)
        //{
        //    StringBuilder sbHtml = new StringBuilder();
        //    string strHtml =context.Cache["user_votecomment_row"].ToString();
        //    string strDel = @"<a href=""javascript:myAjax('/Vote/VoteCommentDel.ashx','CommentID='+$CommentID$);"" class=""imgdel"" ><img src=""/images/del_blog.gif"" alt=""删除"" /></a>";

        //    string str = strHtml;
        //    str = str.Replace("$imgdel$", strDel);
        //    str = str.Replace("$AddTime$",mVoteComm.AddTime.ToString());
        //    ///str = str.Replace("$UserName$",mVoteComm. dtVoteComment.Rows[i]["UserName"].ToString());
        //    str = str.Replace("$UserID$",mVoteComm.FUser.ToString());
        //    str = str.Replace("$Portrait$", mVoteComm..ToString());
        //    str = str.Replace("$CommentContent$",mVoteComm.CommentContent);
        //    ///str = str.Replace("$CommentID$", mVoteComm.c dtVoteComment.Rows[i]["ID"].ToString());

        //    sbHtml.Append(str);
        //    return sbHtml.ToString();
        //}
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
